/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ourpkr.model.joueur;

import java.beans.PropertyChangeSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
import ourpkr.OurPkrApp;

/**
 *
 * @author niboucha
 */
public class Humain extends Joueur {

    Boolean acted = new Boolean(false);

    public Humain(String nom, int nb) {
        pcs = new PropertyChangeSupport(this);
        this.nom = nom;
        this.number = nb;
    }

    //Synchronized = on verrouille la classe pendant l'appel de la fonction
    public synchronized void playAction(String act, int mise) {
        OurPkrApp.getApplication().getPview().getMe().lock();
        if (act.contentEquals("fold")) {
            setWhatToDo(-1);
            acted = true;
        } else if (act.contentEquals("check")) {
            if (fortune + getMiseCourante() - miseMiniTour() >= 0) {
                setWhatToDo(0);
                acted = true;
            } else {
                message("Vous n'avez pas assez d'argent");
            }
        } else if (act.contentEquals("raise")) {
            if (mise > miseMiniTour()) {
                if (fortune + getMiseCourante() - mise >= 0) {
                    setWhatToDo(mise);
                    acted = true;
                }
                else {
                    message("Vous n'avez pas assez d'argent");
                }
            } else {
                message("Le montant minimum du raise est de " + miseMiniTour());
            }
        }
        if (!acted) {
            OurPkrApp.getApplication().getPview().getMe().unlock();
        } else {
            notifyAll();
        }
    }

    public synchronized int joue() {
        setPlaying(true);
        acted = false;
        OurPkrApp.getApplication().getPview().getMe().unlock();
        while (!acted) {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Humain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        setPlaying(false);
        return getWhatToDo();
    }
}
